package com.example.healthapp.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.healthapp.R;
import com.example.healthapp.utils.InterfaceTool;

// 计步器页面的Fragment
public class WalkCountActivity extends AppCompatActivity {

    ImageView walk_count_back;
    TextView walk_count_count, walk_count_kcal, walk_count_start, walk_count_stop;
    int walkCount = 0;
    boolean isRun = true;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walk_count);
        InterfaceTool.setFullScreen(this, true);
        initview();
        event();
    }

    private void event() {
        walk_count_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        walk_count_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                walk_count_start.setEnabled(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (isRun) {
                            walkCount++;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    walk_count_count.setText(walkCount + "");
                                    walk_count_kcal.setText("燃烧了 " + (float) walkCount * 0.04 + " kcal");
                                }
                            });
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }).start();
            }
        });
        walk_count_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                walkCount = 0;
                isRun = false;
                walk_count_start.setEnabled(true);
                walk_count_count.setText("0");
                walk_count_kcal.setText("燃烧了 0 kcal");
            }
        });
    }

    private void initview() {
        walk_count_count = findViewById(R.id.walk_count_count);
        walk_count_kcal = findViewById(R.id.walk_count_kcal);
        walk_count_start = findViewById(R.id.walk_count_start);
        walk_count_stop = findViewById(R.id.walk_count_stop);
        walk_count_back = findViewById(R.id.walk_count_back);
        handler = new Handler();
    }
}